package view.backend.customer;

import exceptionManager.ExceptionManager;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.*;
import view.generaldesign.DesignLayout;

/**
 * 
 * @author Sonny Sandberg
 */
public class AdminCustomer 
{
    private JPanel 
            adminCustomerPanel,
            leftPanel,
            existingCustomersPanel,
            newCustomerPanel,
            scrollPanel;
    private JLabel
            lblPhone,
            lblName,
            lblMortician,
            lblStreet,
            lblZipCity;
    private AdminCustomerTelephone adminTelephone;
    private JButton btnConfirm;    
    private model.Address addressModel;
    private model.Telephone telephoneModel;
    private model.CustomerName customerModel;
    private AdminCustomerName adminCustomerName;
    private AdminCustomerAddress adminAddress;
    private JScrollPane scrollPane;

    public AdminCustomer()
    {
        addressModel = new Address();
        customerModel = new CustomerName();
        telephoneModel = new Telephone();
        telephoneModel.setCustomer(customerModel);
        
        adminCustomerPanel = new JPanel(null);
        adminCustomerPanel.setBounds(395, 320, 650, 500);
        adminCustomerPanel.setOpaque(false);
        
        existingCustomersPanel = new JPanel(null);
        newCustomerPanel = new JPanel(null);
        
        adminTelephone = new AdminCustomerTelephone(this);
        JPanel phonePanel = adminTelephone.getPanel();
        phonePanel.setBounds(0, 0, 300, 85);
        phonePanel.setVisible(true);
        
        buildLeftSide();
        buildNewCustomerPanel();
        buildExistingCustomersPanel();

        adminCustomerPanel.add(phonePanel);
        adminCustomerPanel.setVisible(false);
    }
    
    private void buildLeftSide()
    {
        leftPanel = new JPanel(null);
        leftPanel.setBounds(0, 90, 340, 400);
        leftPanel.setOpaque(false);

        adminCustomerName = new AdminCustomerName(this);
        JPanel customerPanel = adminCustomerName.getPanel();
        customerPanel.setBounds(0, 0, 300, 105);
        
        adminAddress = new AdminCustomerAddress(this);
        JPanel addressPanel = adminAddress.getPanel();
        addressPanel.setBounds(0, 115, 300, 135);
        
        leftPanel.add(customerPanel);
        leftPanel.add(addressPanel);
        leftPanel.setVisible(false);
        
        adminCustomerPanel.add(leftPanel);
    }
    
    public void updateLeftPanel()
    {
        adminAddress.setFieldsFromModel();
        adminCustomerName.setFieldsFromModel();        
    }
    
    public void clearCustomerAndAddressFields()
    {
        addressModel.clearModel();
        customerModel.clearModel();
        updateLeftPanel();
    }
    
    private void buildNewCustomerPanel()
    {
        newCustomerPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Ny Kunde", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        newCustomerPanel.setBounds(310, 0, 340, 195);
        newCustomerPanel.setOpaque(false);
        
        lblPhone = new JLabel("<html><b>Telefon:</b> ");
        lblPhone.setFont(DesignLayout.STANDARD);
        lblPhone.setBounds(10, 35, 290, 20);
        
        lblName = new JLabel("<html><b>Navn:</b> ");
        lblName.setFont(DesignLayout.STANDARD);
        lblName.setBounds(10, 55, 290, 20);
        
        lblMortician = new JLabel("<html><b>Bedemand:</b> </html>");
        lblMortician.setFont(DesignLayout.STANDARD);
        lblMortician.setBounds(10, 75, 290, 20);
        
        lblStreet = new JLabel("Indtast Gade");
        lblStreet.setFont(DesignLayout.STANDARD);
        lblStreet.setBounds(10, 95, 250, 20);
        
        lblZipCity = new JLabel("Indtast Postnummer");
        lblZipCity.setFont(DesignLayout.STANDARD);
        lblZipCity.setBounds(10, 115, 290, 20);
        
        btnConfirm = new JButton("opret kunde");
        btnConfirm.setBounds(200, 150, 130, 35);
        btnConfirm.setEnabled(false);
        btnConfirm.setBorder(DesignLayout.T_FIELD_RED);
        btnConfirm.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnConfirm.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                CustomerSpecified customerSpecified = new CustomerSpecified(customerModel, telephoneModel, addressModel);
                try                
                {
                    boolean done = control.CustomerControl.addNewCustomer(customerSpecified);
                    if (done == true)
                    {                        
                        CustomerSpecified[] customerSpecifiedArray = 
                                model.handlers.CustomerHandler.getSpecifiedCustomersByNumber(customerSpecified.getCustomerTelephone().getNumber());
                        buildFoundPanel(customerSpecifiedArray);
                        clearCustomerAndAddressFields();
                        setLeftPanelVisible(false);
                        setNewCustomerPanelVisible(false);
                        setExistingCustomerPanelVisible(true);
                    }
                    else
                    {
                        // To be handled later
                    }
                } 
                catch (SQLException ex)
                {
                    ExceptionManager.handleSQLException(ex);
                } 
                catch (ClassNotFoundException ex)
                {
                    ExceptionManager.handleClassNotFoundException(ex);
                }
            }
        });
        
        newCustomerPanel.add(lblPhone);
        newCustomerPanel.add(lblName);
        newCustomerPanel.add(lblMortician);
        newCustomerPanel.add(lblStreet);
        newCustomerPanel.add(lblZipCity);
        newCustomerPanel.add(btnConfirm);
        newCustomerPanel.setVisible(false);
        adminCustomerPanel.add(newCustomerPanel);
    }
    
    public void updateNewCustomerPanel()
    {
        lblPhone.setText("<html><b>Telefon:</b> "+telephoneModel.getNumber()+"</html>");
        lblName.setText("<html><b>Navn:</b> "+customerModel.getName()+"</html>");
        
        String mortOrNot = "";
        if (customerModel.isMortician() == true)
        {
            mortOrNot = "Ja";
        }
        else
        {
            mortOrNot = "Nej";
        }
        lblMortician.setText("<html><b>Bedemand: </b>"+mortOrNot+"</html>");
        lblStreet.setText(addressModel.getStreet());
        lblZipCity.setText(addressModel.getZipCode().toString());
        
        if (telephoneModel.isCompleted() == true && addressModel.isCompleted() == true)
        {
            btnConfirm.setEnabled(true);
        }
        else
        {
            btnConfirm.setEnabled(false);
        }
        newCustomerPanel.setVisible(true);
    }
    
    private void buildExistingCustomersPanel()
    {
        existingCustomersPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Vælg Venligst", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        existingCustomersPanel.setBounds(310, 0, 340, 500);
        existingCustomersPanel.setOpaque(false);
        
        scrollPanel = new JPanel(null);
        scrollPanel.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        scrollPanel.setBackground(Color.white);
        scrollPanel.setBorder(null);
        
        scrollPane = new JScrollPane();
        scrollPane.setBorder(DesignLayout.T_FIELD_RED);
        scrollPane.setBounds(20, 34, 300, 450);
        scrollPane.setViewportView(scrollPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        
        existingCustomersPanel.add(scrollPane);
        existingCustomersPanel.setVisible(false);
        adminCustomerPanel.add(existingCustomersPanel);
    }
    
    public void buildFoundPanel(CustomerSpecified[] customerSpecified)
    {
        scrollPanel.removeAll();
        scrollPane.getViewport().setViewPosition(new Point(0,0));
        
        for (int i = 0; i < customerSpecified.length; i++)
        {
            AdminCustomerPanel panel = new AdminCustomerPanel(this, customerSpecified[i]);
            panel.setBounds(10, 10 + (i * 140), 260, 130);
            scrollPanel.setPreferredSize(new Dimension(270, 150 + (i * 140)));
            scrollPanel.add(panel);
        }
        
        scrollPanel.revalidate();
        scrollPanel.repaint();       
    }

    public AdminCustomerTelephone getAdminTelephone()
    {
        return adminTelephone;
    }
    
    public void setNewCustomerPanelVisible(boolean visible)
    {
        newCustomerPanel.setVisible(visible);
    }
    
    public void setExistingCustomerPanelVisible(boolean visible)
    {
        existingCustomersPanel.setVisible(visible);
    }
    
    public void setLeftPanelVisible(boolean visible)
    {
        leftPanel.setVisible(visible);
    }

    public CustomerName getCustomerModel()
    {
        return customerModel;
    }

    public void setCustomerModel(CustomerName customer)
    {
        this.customerModel = customer;
    }

    public Address getAddressModel()
    {
        return addressModel;
    }

    public void setAddressModel(Address addressModel)
    {
        this.addressModel = addressModel;
    }

    public Telephone getTelephoneModel()
    {
        return telephoneModel;
    }

    public void setTelephoneModel(Telephone telephoneModel)
    {
        this.telephoneModel = telephoneModel;
    }

    public JPanel getPanel()
    {
        return adminCustomerPanel;
    }
}